package com.mezz.bones.framework.mybatis.dynamic.logging;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.stereotype.Component;

@Component
public class DataSourceAwareStdOutImpl implements Log {
    private final Log delegate;

    public DataSourceAwareStdOutImpl() {
        this(DataSourceAwareStdOutImpl.class.getName());
    }

    public DataSourceAwareStdOutImpl(String clazz) {
        this.delegate = new StdOutImpl(clazz);
    }
    @Override
    public boolean isDebugEnabled() {
        return delegate.isDebugEnabled();
    }

    @Override
    public boolean isTraceEnabled() {
        return delegate.isTraceEnabled();
    }

    @Override
    public void error(String s, Throwable e) {
        delegate.error(s, e);
    }

    @Override
    public void error(String s) {
        delegate.error(s);
    }

    @Override
    public void debug(String s) {
        String currentDs = DynamicDataSourceContextHolder.peek();
        delegate.debug("[数据源: " + (currentDs != null ? currentDs : "默认") + "] " + s);
    }

    @Override
    public void trace(String s) {
        delegate.trace(s);
    }

    @Override
    public void warn(String s) {
        delegate.warn(s);
    }
}